const path = require('path')
//项目默认配置
const defaultSettings = require('./src/settings.js');
//是否生产环境
const isProduction = process.env.NODE_ENV === "production";
//是否开发环境
const isDevelopment = process.env.NODE_ENV === "development";


function resolve(dir) {
    return path.join(__dirname, dir)
}

const name = defaultSettings.title // page title


// All configuration item explanations can be find in https://cli.vuejs.org/config/
module.exports = {
    /**
     * You will need to set publicPath if you plan to deploy your site under a sub path,
     * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
     * then publicPath should be set to "/bar/".
     * In most cases please use '/' !!!
     * Detail: https://cli.vuejs.org/config/#publicpath
     */
    //相对路径
    publicPath: './',
    // publicPath: (process.env.NODE_ENV === 'development' ? '/' : './'),
    outputDir: 'dist',
    assetsDir: 'static',
    // lintOnSave: process.env.NODE_ENV === 'development',
    lintOnSave: false,
    filenameHashing: true, // 文件名哈希
    productionSourceMap: false,
    runtimeCompiler: true,// 设置为 true 后你就可以在 Vue 组件中使用 template 选项
    //开发环境服务
    devServer: {
        port: 8888,
        // open: true,
        open: false,
        //关闭域名检查
        disableHostCheck: true,
        // 本地开发代理
        proxy: {
            '/': {
                target: 'http://localhost:9696',
                changeOrigin: true
            }
        },
        overlay: {
            warnings: false,
            errors: true
        },
        // before: require('./mock/mock-server.js')
    },
    css: {
        extract: true,
    },
    configureWebpack: config => {

        // 不是开发模式，是其他生产模式下
        if (!isDevelopment) {
            config.output.filename = `js/[name].[contenthash].js`
            config.output.chunkFilename = `js/[name].[contenthash].js`
        }
        config.name = name;
        config.resolve.alias = {
            '@': resolve('src'),
            'vue$': 'vue/dist/vue.esm.js',
        };
    },
    chainWebpack(config) {
        // 不是开发模式，是其他生产模式下
        if (!isDevelopment) {

            // 去掉元素之间空格
            config.module
                .rule("vue")
                .use("vue-loader")
                .loader("vue-loader")
                .tap(options => {
                    options.compilerOptions.preserveWhitespace = true;
                    return options;
                })
                .end();

            // 移除 prefetch 插件
            config.plugins.delete("prefetch-index");

            // 移除 preload 插件，避免加载多余的资源
            config.plugins.delete("preload-index");

            config.optimization.minimizer("terser").tap(args => {
                // 去掉注释
                args[0].terserOptions.output = {
                    comments: false
                };
                // 去掉多余console
                // args[0].terserOptions.compress = {
                //         warnings: false,
                //         drop_console: true,
                //         drop_debugger: true,
                //         pure_funcs: ['console.log']
                // }
                return args;
            });

            config.plugin('html')
                .tap(args => {
                    args[0].title = name;
                    return args
                });
        }
    }
}
